/**
 * 
 */
package com.feib.stms.service.impl;

import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.feib.stms.dao.MenuItemDao;
import com.feib.stms.dao.RoleDao;
import com.feib.stms.dao.UserDao;
import com.feib.stms.model.Group;
import com.feib.stms.model.MenuItem;
import com.feib.stms.model.Role;
import com.feib.stms.model.User;
import com.feib.stms.pagination.PaginatedList;
import com.feib.stms.service.UserService;
import com.feib.stms.util.UserUtil;

/**
 * @author Jimmy Liu
 *
 */
@Service("userService")
public class UserServiceImpl extends BaseServiceImpl implements UserService 
{
	private static final long serialVersionUID = 8647168765522835279L;

	@Resource(name="userDao")
	private UserDao userDao;
	
	@Resource(name="roleDao")
    private RoleDao roleDao;
	
	@Resource(name="menuItemDao")
    private MenuItemDao menuItemDao;

	public User findLoginDataByUserIdGroupNo(String userId, String groupNo) 
	{
		
		User user = this.findByUserIdGroupNo(userId, groupNo);
		
		if (null != user)
		{
			// 取得 角色資料
			List<Role> dbRoles = roleDao.getByUserPk(user);
			Set<Role> roles = new LinkedHashSet<Role>(dbRoles.size());
			user.setRoles(roles);
			for(Role role : dbRoles)
			{
				List<MenuItem> dbMenuItems = menuItemDao.getByRole(role);
				Set<MenuItem> menuItems = new LinkedHashSet<MenuItem>(dbMenuItems.size());
				for(MenuItem menuItem : dbMenuItems)
				{
					menuItems.add(menuItem);
				}
				role.setMenuItems(menuItems);
				roles.add(role);
			}
		}
		
		return user;
	}

	public int loginFail(User user) {		
		return userDao.doLoginFail(user);
	}

	public User findByUserIdGroupNo(String userId, String groupNo) {
		User queryUser = new User();
		queryUser.setUserId(userId);
		queryUser.setGroup(new Group());
		queryUser.getGroup().setGroupNo(groupNo);
		
		return userDao.getByUserIdGroupNo(queryUser);
	}

	public int loginSuccess(User user) {
		
		Date now = new Date();
		user.setEnabled(Boolean.TRUE);
		user.setLocked(Boolean.FALSE);
		user.setPwdErrorTime(new Integer(0));
		user.setCaptchaErrorTime(new Integer(0));
		user.setLastLoginDate(now);
		user.setUpdatedDate(now);
		
		User loginUser = UserUtil.getUser();
		user.setUpdatedDate(new Date());
		user.setUpdatedBy(loginUser.getGroup().getGroupNo() +"_" + loginUser.getUserId());
		
		return userDao.doLoginSuccess(user);
	}

	public User findByUerPk(Long userPk) {
		return userDao.getByUserPk(userPk);
	}

	public int changePwdSuccess(User user) {
		return userDao.doChangePwdSuccess(user);		
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.feib.stms.service.UserService#queryByUser(com.feib.stms.model.User, com.feib.stms.pagination.PaginatedList)
	 */
	public PaginatedList<User> queryByUser(User user, PaginatedList<User> paginationConfig) {	
		return userDao.queryForPaginatedListByUser(user, paginationConfig);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.feib.stms.service.UserService#countUserId(java.lang.String, java.lang.Long)
	 */
	public int countUserId(String userId,Long groupPk) {
		return userDao.countUserId(userId,groupPk);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.feib.stms.service.UserService#countUserId(java.lang.Long, java.lang.String, java.lang.Long)
	 */
	public int countUserId(Long userPk, String userId,Long groupPk) {
		return userDao.countUserId(userPk, userId,groupPk);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.feib.stms.service.UserService#saveUser(com.feib.stms.model.User)
	 */
	public int saveUser(User user) {
		return userDao.insert(user);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.feib.stms.service.UserService#updateUser(com.feib.stms.model.User)
	 */
	public int updateUser(User user) {
		return userDao.update(user);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.feib.stms.service.UserService#findByUerId(java.lang.String, java.lang.Long)
	 */
	public User findByUerId(String userId,Long groupPk) {
		return userDao.findByUserId(userId,groupPk);
	}
}
